package com.handinglian.servicerecord.controller;

import com.apidoc.annotation.Api;
import com.apidoc.annotation.ApiVersion;
import com.handinglian.common.dto.CommonPage;
import com.handinglian.common.dto.ResultData;
import com.handinglian.common.factory.ResultDataFactory;
import com.handinglian.common.shiro.Principal;
import com.handinglian.servicerecord.dto.MessageDto;
import com.handinglian.servicerecord.dto.MessageTemplateDto;
import com.handinglian.servicerecord.param.MessageTemplateCreateParam;
import com.handinglian.servicerecord.param.MessageTemplateUpdateParam;
import com.handinglian.servicerecord.service.MessageService;
import com.handinglian.servicerecord.service.MessageTemplateService;
import com.handinglian.task.param.MessageParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@Api("短信")
@RestController
@Slf4j
@RequestMapping("/{version}/message")
@ApiVersion(1.0)
public class MessageController {
    @Autowired
    private MessageTemplateService messageTemplateService;
    @Autowired
    private MessageService messageService;

    /**
     * 创建短信模板
     */
    @RequiresPermissions("message_template:create")
    @PostMapping("/createTemplate")
    public ResultData createTemplate(Principal principal, @RequestBody MessageTemplateCreateParam createParam){
        Integer amount = messageTemplateService.create(createParam, principal.getUserId());
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 删除短信模板
     */
    @RequiresPermissions("message_template:delete")
    @DeleteMapping(value = "/deleteTemplate")
    public ResultData deleteTemplate(Integer id) {
        int amount = messageTemplateService.delete(id);
        return ResultDataFactory.generateResultData(amount);
    }

    /**
     * 更新短信模板
     */
    @RequiresPermissions("message_template:update")
    @PutMapping(value = "/updateTemplate")
    public ResultData updateTemplate(@RequestBody MessageTemplateUpdateParam taskInfoUpdateParam) {
        int amount = messageTemplateService.update(taskInfoUpdateParam);
        return ResultDataFactory.generateResultData(amount);
    }


    /**
     * 查询短信模板详情
     */
    @RequiresPermissions("message_template:list")
    @GetMapping(value = "/getTemplate")
    public ResultData<MessageTemplateDto> getTemplate(Integer id) {
        MessageTemplateDto messageTemplateDto = messageTemplateService.get(id);
        return ResultDataFactory.generateSuccessResultData(messageTemplateDto);
    }

    /**
     * 获取短信模板列表
     */
    @RequiresPermissions("message_template:list")
    @GetMapping("/listTemplate")
    public ResultData<CommonPage<MessageTemplateDto>> listTemplate(String nameOrContent, String sortField, String sortOrder, Integer pageIndex, Integer pageSize) {
        CommonPage<MessageTemplateDto> messageTemplateDtos = messageTemplateService.list(nameOrContent, sortField, sortOrder, pageIndex, pageSize);
        return ResultDataFactory.generateSuccessResultData(messageTemplateDtos);
    }

    /**
     * 查询短信详情
     */
    @RequiresPermissions("message_template:list")
    @GetMapping(value = "/get")
    public ResultData<MessageDto> get(Integer id) {
        MessageDto messageDto = messageService.get(id);
        return ResultDataFactory.generateSuccessResultData(messageDto);
    }

    /**
     * 获取短信列表
     */
    @RequiresPermissions("message_template:list")
    @GetMapping("/list")
    public ResultData<CommonPage<MessageDto>> list(Principal principal, String userName, String customerNameOrPhone, String titleOrContent, String startDate, String endDate, String sortField, String sortOrder, Integer pageIndex, Integer pageSize) {
        CommonPage<MessageDto> messageDtos = messageService.list(principal.getUserId(), userName, customerNameOrPhone, titleOrContent, startDate, endDate, sortField, sortOrder, pageIndex, pageSize);
        return ResultDataFactory.generateSuccessResultData(messageDtos);
    }

    /**
     * 单条短信发送
     */
    @RequiresPermissions("message_template:create")
    @PostMapping("/sendSingleMessage")
    public ResultData sendSingleMessage(Principal principal, @RequestBody MessageParam messageParam) throws IOException {
        int amount = messageService.sendSingleMessage(principal.getUserId(), messageParam);
        return ResultDataFactory.generateSuccessResultData(amount);
    }
}
